/* 
**    Copyright (C) 2012 Institute for Systems Biology 
**                       Seattle, Washington, USA. 
**
**    This library is free software; you can redistribute it and/or
**    modify it under the terms of the GNU Lesser General Public
**    License as published by the Free Software Foundation; either
**    version 2.1 of the License, or (at your option) any later version.
**
**    This library is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**    Lesser General Public License for more details.
**
**    You should have received a copy of the GNU Lesser General Public
**    License along with this library; if not, write to the Free Software
**    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

var pdd ={};

/***************************************************************
**
** This object does patient data display
*/

pdd.PatientDataDisplay = function(canvasID) {
  this.canvasID = canvasID;
  this.myPoints = [];
  this.name1 = null;
  this.name2 = null;
  this.minMaxX = {min: Number.MAX_VALUE, max: Number.MIN_VALUE};
  this.minMaxY = {min: Number.MAX_VALUE, max: Number.MIN_VALUE};
};

/***************************************************************
**
** Reset shared bounds
*/

pdd.PatientDataDisplay.prototype.reset = function() {
  this.minMaxX = {min: Number.MAX_VALUE, max: Number.MIN_VALUE};
  this.minMaxY = {min: Number.MAX_VALUE, max: Number.MIN_VALUE};
  this.name1 = null;
  this.name2 = null;
  this.myPoints = [];
  return;
};

/***************************************************************
**
** Register all displays
*/

pdd.PatientDataDisplay.prototype.register = function(allDisplays, myIndex) {
  this.allDisplays = allDisplays;
  this.myIndex = myIndex;
  return;
};

/***************************************************************
**
** Let us know about everybody's range
*/

pdd.PatientDataDisplay.prototype.setRange = function(otherMinMaxX, otherMinMaxY) {
  if (otherMinMaxX.max > this.minMaxX.max) {
    this.minMaxX.max = otherMinMaxX.max;
  }
  if (otherMinMaxX.min < this.minMaxX.min) {
    this.minMaxX.min = otherMinMaxX.min;
  }
  if (otherMinMaxY.max > this.minMaxY.max) {
    this.minMaxY.max = otherMinMaxY.max;
  }
  if (otherMinMaxY.min < this.minMaxY.min) {
    this.minMaxY.min = otherMinMaxY.min;
  }
  return;
};

/***************************************************************
**
** Install and render the data
*/

pdd.PatientDataDisplay.prototype.installData = function(thePoints, name1, name2) {
  this.myPoints = [];
  for (var i = 0; i < thePoints.length; i++) {
    this.myPoints[i] = {};
    this.myPoints[i].x = thePoints[i].x * 100.0;
    this.myPoints[i].y = thePoints[i].y * 100.0;
  }
  
  for (var i = 0; i < this.myPoints.length; i++) {
    if (this.myPoints[i].x > this.minMaxX.max) {
      this.minMaxX.max = this.myPoints[i].x;
    }
    if (this.myPoints[i].x < this.minMaxX.min) {
      this.minMaxX.min = this.myPoints[i].x;
    }
    if (this.myPoints[i].y > this.minMaxY.max) {
      this.minMaxY.max = this.myPoints[i].y;
    }
    if (this.myPoints[i].y < this.minMaxY.min) {
      this.minMaxY.min = this.myPoints[i].y;
    }
  };
  this.name1 = name1;
  this.name2 = name2;
  for (var i = 0; i < this.allDisplays.length; i++) {
    if (i === this.myIndex) {
      continue;
    }
    this.allDisplays[i].setRange(this.minMaxX, this.minMaxY);
    this.allDisplays[i].render();
  }
  return;
};


/***************************************************************
**
** Renders the data
*/

pdd.PatientDataDisplay.prototype.render = function() {
 
  var theCanvas = document.getElementById(this.canvasID);
  
  var context = theCanvas.getContext("2d");
  var width = theCanvas.getAttribute('width');
  var height = theCanvas.getAttribute('height');
  
  context.setTransform(1,0,0,1,0,0);
	context.fillStyle = "#ffffff";
	context.fillRect(0, 0, width, height); 
	context.strokeStyle = "#000000";
	context.lineWidth = 0.2;
	context.strokeRect(0, 0, width, height);
	
  context.textAlign = "center";
  context.textBaseline = "middle";
  context.font = "bold 10px 'Times New Roman', Times, serif";
  context.fillStyle = "#333";
 
  context.fillText(this.name1, width / 2.0, height - 10.0);
 
  context.translate(10.0, (height / 2.0));
  context.rotate(-Math.PI / 2.0); 
  context.fillText(this.name2, 0, 0);
   
  var empty = (this.myPoints.length === 0);
   
  var scaleFacX = (empty) ? 1.0 : (width / (this.minMaxX.max - this.minMaxX.min)); 
  var scaleFacY = (empty) ? 1.0 : (height / (this.minMaxY.max - this.minMaxY.min));
  var minScale = (scaleFacX < scaleFacY) ? scaleFacX : scaleFacY;
  minScale *= 0.66;
  var centerX = (empty) ? (width / 2.0) : ((this.minMaxX.max + this.minMaxX.min) / 2.0);
  var centerY = (empty) ? (height / 2.0) : ((this.minMaxY.max + this.minMaxY.min) / 2.0);
  
  context.setTransform(1,0,0,1,0,0);   
 	context.translate((width / 2.0), (height / 2.0));
	context.scale(minScale, -minScale);
	context.translate(-centerX, -centerY);
	var dia = 0.2 / minScale;
	var lw = 2.0 / minScale;
  
  for (var i = 0; i < this.myPoints.length; i++) {
    context.beginPath();
    context.strokeStyle = "#FF0000";
    context.lineWidth = lw;
    context.arc(this.myPoints[i].x, this.myPoints[i].y, dia, 0.0, Math.PI * 2.0, false);
    context.stroke();
    context.closePath();
  };
 
  return;
};